<template>
  <div>
    <!-- 旧件回运单签收-->
    <BreakSpace label="回运单信息" :containerStyle="{ marginBottom: '10px' }" />
    <FormPanel ref="formPanel1" :list="formList1" :cols="3" :formType="'add'" labelWidth="100" :isSubmitBtn="false" @formChange="formChangeHandle" />
    <div style="padding-top: 3rem;"></div>

    <BreakSpace label="上报信息" :containerStyle="{ marginBottom: '10px' }" />
    <FormPanel ref="formPanel2" :list="formList2" :cols="3" :formType="'add'" labelWidth="110" :isSubmitBtn="false" />
    <div style="padding-top: 3rem;"></div>

    <BreakSpace label="费用信息" :containerStyle="{ marginBottom: '10px' }" />
    <FormPanel ref="formPanel3" :list="formList3" :cols="3" :formType="'add'" labelWidth="110" :isSubmitBtn="false" @formChange="formChangeHandle2"/>

    <!--    <BreakSpace label="旧件列表" :containerStyle="{ marginBottom: '10px' }" />-->
    <!--    <FilterTable-->
    <!--      ref="table1"-->
    <!--      :columns="columns"-->
    <!--      :params="fetchParams"-->
    <!--      :dataSource="dataList"-->
    <!--      :datakey="'items'"-->
    <!--      :onColumnsChange="columns => (this.columns = columns)"-->
    <!--      :onRowSelectChange="selectChange"-->
    <!--      selectionType="single"-->
    <!--      height="auto"-->
    <!--      :isPagination=false-->
    <!--      :isSelectColumn=false-->
    <!--    >-->
    <!--    </FilterTable>-->
    <!--    <div style="padding-top: 3rem;"></div>-->
    <!--    <BreakSpace label="操作记录" :containerStyle="{ marginBottom: '10px' }" />-->
    <!--    <FilterTable-->
    <!--      ref="table2"-->
    <!--      :columns="caozuojilucolumns"-->
    <!--      :params="fetchParams"-->
    <!--      :dataSource="dataList1"-->
    <!--      :datakey="'items'"-->
    <!--      :onColumnsChange="columns => (this.columns = columns)"-->
    <!--      :onRowSelectChange="selectChange"-->
    <!--      selectionType="single"-->
    <!--      height="auto"-->
    <!--      :isPagination=false-->
    <!--      :isSelectColumn=false-->
    <!--    >-->
    <!--    </FilterTable>-->
    <div style="margin-bottom: 1rem;"></div>
    <div style="padding-top: 3rem;"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <multiuse-button type="primary" :click="sign.bind()" >签收</multiuse-button>
      <multiuse-button @click="closeDialog">关 闭</multiuse-button>
    </div>
  </div>
</template>
<script>
import { getPartType } from '@/api/dictionary/index.js';
import {findFileByUrl, selectById} from '@/api/usedPartsReturnManage/usedPartsReturn_OEM/usedPartsWarehousing';
import { dictionary } from '@/utils/dictMixin';
import {newSave, sign} from "@/api/usedPartsReturnManage/usedPartsReturn_OEM/usedPartsSigning";
export default {
  props: ['rowData'],
  mixins: [dictionary],
  components: {

  },
  data() {
    return {
      uploadJfHiddenFlag: true,
      jfDisableButton: true,
      loading:false,
      dataList: [],
      dataList1: [],
      visible: false,
      visibleDrawer: false,
      selectDelearFlag: false,
      collapse: false,
      infoId: null,
      flterValue: {}, //参数
      //上报信息
      // columns: this.creatHeader(),
      //操作记录
      // caozuojilucolumns: this.caozuojilu(),
      fetchParams: {}, //ipa 接口
      selectedRows: [], //选中服务站
      //通知单信息
      formList1: this.createFormList(),
      formList2: this.createformListshangbao(),
      formList3: this.createformListAmount(),
      saveData: null,
      saveData2: null,
      saveData3: null
    };
  },
  created() {
    // this.chuqi();
    this.chuqi2();
    this.chuqi3();
    this.chuqi4();
    this.chuqi5();
    this.init(this.rowData.returnId);
  },
  methods: {
    // 获取服务端文件 to blob
    async downLoadByUrl(url, params = {}) {
      let res = await findFileByUrl(url);
      console.log(res.data)
      return res.data;
    },
    async init(val) {
      let newVar = await selectById({id: val})
      if (newVar.data.returnType === 55951001) {
        this.jfDisableButton = false;
      } else {
        this.uploadJfHiddenFlag = false;
      }
      this.editData = newVar.data;
      console.log("=== 通过ID查询结果返回 ==== ",newVar," ======");
      this.formList1.forEach(x => {
        if (this.jfDisableButton) {
          if (x.fieldName == 'wholeReturn') {
            x.hidden = false;
          }
          if ((x.fieldName == 'pickUpPerson' || x.fieldName == 'certificateNo') && newVar.data.expressType === 55211004) {
            x.hidden = false;
          }
          if ((x.fieldName == 'expressAddress' || x.fieldName == 'cPerson' || x.fieldName == 'cTel') && newVar.data.expressType === 55211004) {
            x.hidden = true;
          }
        }
        console.log(newVar);
        x.initialValue = newVar.data[x.fieldName];
        if (x.fieldName == 'partNum') {
          x.initialValue = this.rowData.partNum;
        }
      });

      //文件地址转换
      // let tempFileArr =[];
      // for (let i = 0; i < newVar.data.fileList.length; i++) {
      //   let current = newVar.data.fileList[i];
      //   let res = await findFileByUrl(current.fileUrl);
      //   if (res) {
      //     current.fileUrl = res.data;
      //     tempFileArr.push(current);
      //   }
      // }
      for (const x of this.formList2) {
        if (this.jfDisableButton) {
          if (x.type == 'BREAK_SPACE') {
            x.hidden = false;
          }
          if ((x.fieldName == 'certificateNoPic' || x.fieldName == 'pickUpPic' || x.fieldName == 'partPic') && newVar.data.expressType === 55211004) {
            x.hidden = false;
          }
          if ((x.fieldName == 'expressPic') && newVar.data.expressType === 55211005) {
            x.hidden = false;
          }
        }
        if (this.jfDisableButton && (x.fieldName == 'certificateNoPic' || x.fieldName == 'pickUpPic' || x.fieldName == 'partPic' || x.fieldName == 'expressPic')) {
          if (newVar.data[x.fieldName] == null) {
            x.initialValue = [];
          } else {
            let realUrl = newVar.data[x.fieldName].split("?")[0]
            let picUrl = await this.downLoadByUrl(realUrl)
            x.initialValue = [picUrl];
          }
        } else {
          x.initialValue = newVar.data[x.fieldName];
        }
        if (x.fieldName == 'fileList') {
          x.initialValue = newVar.data.fileList.map((f) => ({name: f.title, url: f.fileUrl}));
        }
      };

      this.formList3.forEach(x => {
        if (this.jfDisableButton) {
          if (x.fieldName == 'jfFreightAmount') {
            x.hidden = false;
          }
          if (x.fieldName == 'freightAmount') {
            x.hidden = true;
          }
        }
        x.initialValue = newVar.data[x.fieldName];
      });
      // this.$refs.table1.$refs.pageTable.EXECUTE_INSERT(newVar.data.dtlList);
      // this.$refs.table2.$refs.pageTable.EXECUTE_INSERT(newVar.data.logList);
      if(this.rowData.statusStr ==='已回运'){
        this.formList1.find(x => x.fieldName == 'str').hidden = false;
        this.formList1.find(x => x.fieldName == 'str').initialValue = this.rowData.statusStr;
        this.flag = false;
      }else{
        this.formList1.find(x => x.fieldName == 'str').hidden = true;
        this.formList1.find(x => x.fieldName == 'returnStatus').hidden = false;
      }

    },
    //签收
    async sign() {
      this.$refs.formPanel1.SUBMIT_FORM();
      this.$refs.formPanel2.SUBMIT_FORM();
      this.$refs.formPanel3.SUBMIT_FORM();
      console.log("回运单信息", this.saveData);
      console.log("费用信息", this.saveData2);
      let save = {};
      save = Object.assign({}, this.saveData, this.saveData2);
      this.loading = true;
      let res = await sign(save);
      console.log(res);
      if (res.resultCode === 200) {
        this.$emit('save');
        return this.$notify.success({title: '提示信息', message: '签收成功'});
      } else {
        this.loading = false;
        this.$notify.error({title: '提示信息', message: res.errMsg || '签收失败!'});
        // this.closeDialog();
      }
      this.loading = false;
    },
    //旧件状态
    // async chuqi() {
    //   console.log("===== 获取下拉框数据 ====== ")
    //   const res = await getPartType({ type: 5585 });
    //   console.log("==========",res)
    //   if (res.resultCode == 200) {
    //     this.columns.find(x => x.dataIndex == 'partStatus').initialValue = '';
    //     this.columns.find(x => x.dataIndex == 'partStatus').editItems = res.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
    //   }
    // },
    //运输方式下拉框
    async chuqi2() {
      console.log("===== 获取下拉框数据 ====== ")
      const res = await getPartType({ type: 5521 });
      console.log("==========",res)
      if (res.resultCode == 200) {
        this.formList2.find(x => x.fieldName == 'expressType').initialValue = '';
        this.formList2.find(x => x.fieldName == 'expressType').itemList = res.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
      }
    },
    //单据状态
    async chuqi3() {
      console.log("===== 获取下拉框数据 ====== ")
      const res = await getPartType({ type: 5588 });
      console.log("==========",res)
      if (res.resultCode == 200) {
        this.formList1.find(x => x.fieldName == 'returnStatus').initialValue = '';
        this.formList1.find(x => x.fieldName == 'returnStatus').itemList = res.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
      }
    },
    //入库说明
    async chuqi4() {
      console.log("===== 获取下拉框数据 ====== ")
      const res = await getPartType({ type: 5590 });
      console.log("==========",res)
      if (res.resultCode == 200) {
        this.columns.find(x => x.dataIndex == 'inInfo').initialValue = '';
        this.columns.find(x => x.dataIndex == 'inInfo').editItems = res.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
      }
    },
    async chuqi5() {
      console.log("===== 获取下拉框数据 ====== ")
      const res = await getPartType({ type: 1004 });
      console.log("==========",res)
      if (res.resultCode == 200) {
        this.formList1.find(x => x.fieldName == 'wholeReturn').initialValue = '';
        this.formList1.find(x => x.fieldName == 'wholeReturn').itemList = res.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
      }
    },
    formChangeHandle(val) {
      console.log(val);
      this.saveData = val;
    },
    formChangeHandle2(val) {
      console.log("formChangeHandle2",val);
      this.saveData2= val;
    },
    //搜索获取数据
    changeHandle(val) {
      this.fetchParams = val;
      console.log(val);
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    //保存
    saveDialog() {
      this.$refs.formPanel1.SUBMIT_FORM();
      if (this.saveData) {
        this.$emit('save', this.selectedRows);
      }
    },
    //取消操作
    closeDialog() {
      this.$emit('close', { visible: false, data: this.selectedRows, type: 'close' });
    }, //服务站选择页面
    SelectDelear() {
      this.selectDelearFlag = true;
    }, //关闭
    Delearclose() {
      this.selectDelearFlag = false;
    },
    //保存
    Delearsave() {
      this.selectDelearFlag = false;
      this.fetchParams = { ...this.fetchParams };
    },
    chooseOrderNo() {
      this.visible = true;
    },
    add() {},
    //回运单信息
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '回运单号',
          fieldName: 'returnNo',
          disabled: true, // 置会
          readonly: true // 只读
        },
        {
          type: 'INPUT',
          label: '联系人',
          fieldName: 'cPerson',
          disabled: true, // 置会
          readonly: true // 只读
        },
        {
          type: 'INPUT',
          label: '联系人电话',
          fieldName: 'cTel',
          readonly: true, // 只读
          disabled: true //置灰
        },
        {
          type: 'INPUT',
          label: '取件人',
          placeholder: '请输入取件人...',
          fieldName: 'pickUpPerson',
          readOnly: true,
          disabled: true,
          hidden: true,
        },
        {
          type: 'INPUT',
          label: '取件人身份证',
          fieldName: 'certificateNo',
          readOnly: true,
          disabled: true,
          hidden: true,
        },
        {
          type: 'INPUT',
          label: '旧件仓库',
          fieldName: 'whName',
          disabled: true, // 置会
          readonly: true // 只读
        },
        {
          type: 'INPUT',
          label: '邮寄地址',
          placeholder: '请输入邮寄地址...',
          fieldName: 'expressAddress',
          readonly: true, // 只读
          disabled: true //置灰
        },
        {
          type: 'SELECT',
          label: '是否整单回运',
          fieldName: 'wholeReturn',
          initialValue: '',
          itemList: [],
          disabled: true, // 置会
          readonly: true, // 只读
          hidden: true
        },
      ];
    },
    //上报信息
    createformListshangbao() {
      return [
        {
          type: 'SELECT',
          label: '运输方式',
          fieldName: 'expressType',
          readonly: true, // 只读
          disabled: true, // 置会
          itemList: []
        },
        {
          type: 'INPUT',
          label: '物流公司',
          fieldName: 'expressCompany',
          disabled: true, // 置会
          readonly: true // 只读
        },
        {
          type: 'INPUT',
          label: '物流单号',
          fieldName: 'expressNo',
          disabled: true, // 置会
          readonly: true // 只读
        },
        {
          type: 'INPUT',
          label: '包装箱数',
          fieldName: 'boxCounts',
          disabled: true, // 置会
          readonly: true // 只读
        },
        {
          type: 'INPUT',
          label: '纸箱数量',
          fieldName: 'paperCounts',
          disabled: true, // 置会
          readonly: true // 只读
        },
        {
          type: 'INPUT',
          label: '木箱数量',
          fieldName: 'woodCounts',
          disabled: true, // 置会
          readonly: true // 只读
        },
        {
          type: 'INPUT',
          label: '编织袋数量',
          fieldName: 'ironCounts',
          disabled: true, // 置会
          readonly: true // 只读
        },
        {
          type: 'INPUT',
          label: '裸件数量',
          fieldName: 'bareParts',
          disabled: true, // 置会
          readonly: true // 只读
        },
        {
          type: 'DATE',
          label: '发运时间',
          fieldName: 'dlDate',
          valueFormat: 'yyyy-MM-dd',
          readonly: true, // 只读
          disabled: true // 置会
        },
        {
          type: 'TEXT_AREA',
          label: '备注',
          fieldName: 'remarks',
          readonly: true, // 只读
          disabled: true, // 置会
          selfCols: 3,// 元素占据几行
          rows: 3
        },
        {
          type: 'UPLOAD_FILE',
          label: '查看文件',
          fieldName: 'fileList',
          placeholder: '选择文件...',
          disabled: true, //置灰
          readonly: true,// 只读
          upload: {
            actionUrl: '/api/file/oss/upload',
            limit: 10,
            tipText: '（支持PDF、RAR、JPG格式）'
          },
          hidden: !this.uploadJfHiddenFlag,
        },
        {
          type: 'BREAK_SPACE',
          label: '快返附件',
          hidden: true
        },
        {
          type: 'UPLOAD_IMG',
          label: '保修旧件图片',
          fieldName: 'partPic',
          placeholder: '上传索保修旧件图片...',
          selfCols: 1,
          initialValue: [],
          rules: [{ required: true, message: '请上传索赔件图片', trigger: 'change' }],
          upload: {
            // actionUrl: config.uploadUrl+'/api/file/oss/upload',
            // actionUrl: `${config.uploadUrl}/api/file/v1/file/upload`,
            actionUrl: '/api/file/oss/upload',
            fixedSize: [5, 3],
            limit: 1,
            isCalcHeight: true,
            isFileName: true,
            isPreview:true
          },
          hidden: true,
          disabled: true, //置灰
          readonly: true // 只读
        },
        {
          type: 'UPLOAD_IMG',
          label: '取件人凭证',
          fieldName: 'pickUpPic',
          placeholder: '上传取件人凭证...',
          selfCols: 1,
          initialValue: [],
          rules: [{ required: true, message: '请上传取件人凭证', trigger: 'change' }],
          upload: {
            // actionUrl: config.uploadUrl+'/api/file/oss/upload',
            // actionUrl: `${config.uploadUrl}/api/file/v1/file/upload`,
            actionUrl: '/api/file/oss/upload',
            fixedSize: [5, 3],
            limit: 1,
            isCalcHeight: true,
            isFileName: true,
            isPreview:true
          },
          hidden: true,
          disabled: true, //置灰
          readonly: true // 只读
        },
        {
          type: 'UPLOAD_IMG',
          label: '取件人身份证凭证照片',
          fieldName: 'certificateNoPic',
          placeholder: '上传取件人身份证凭证照片...',
          selfCols: 1,
          initialValue: [],
          rules: [{ required: true, message: '请上传取件人身份证凭证照片', trigger: 'change' }],
          upload: {
            // actionUrl: config.uploadUrl+'/api/file/oss/upload',
            // actionUrl: `${config.uploadUrl}/api/file/v1/file/upload`,
            actionUrl: '/api/file/oss/upload',
            fixedSize: [5, 3],
            limit: 1,
            isCalcHeight: true,
            isFileName: true,
            isPreview:true
          },
          hidden: true,
          disabled: true, //置灰
          readonly: true // 只读
        },
        {
          type: 'UPLOAD_IMG',
          label: '快递凭证',
          fieldName: 'expressPic',
          placeholder: '上传快递凭证...',
          selfCols: 1,
          initialValue: [],
          rules: [{ required: true, message: '请上传快递凭证', trigger: 'change' }],
          upload: {
            // actionUrl: config.uploadUrl+'/api/file/oss/upload',
            // actionUrl: `${config.uploadUrl}/api/file/v1/file/upload`,
            actionUrl: '/api/file/oss/upload',
            fixedSize: [5, 3],
            limit: 1,
            isCalcHeight: true,
            isFileName: true,
            isPreview:true
          },
          hidden: true,
          disabled: true, //置灰
          readonly: true // 只读
        },
        {
          type: 'UPLOAD_FILE',
          label: '查看文件',
          fieldName: 'fileList',
          placeholder: '选择文件...',
          disabled: true, //置灰
          readonly: true,// 只读
          upload: {
            actionUrl: '/api/file/oss/upload',
            limit: 10,
            tipText: '（支持PDF、RAR、JPG格式）'
          },
          hidden: this.uploadJfHiddenFlag,
        },
      ];
    },
    //费用信息
    createformListAmount() {
      return [
        {
          type: 'INPUT_NUMBER',
          label: '紧急返件运费',
          placeholder: '请输入紧急返件运费...',
          fieldName: 'jfFreightAmount',
          rules: [{required: true, message: '请输入请输入紧急回运运费...', trigger: 'change'}],
          initialValue: 0,
          max: 99999999, // 最大值
          min: 0.00, // 最小值
          precision: 2, // 小数精度
          hidden: true,
          readonly: true, // 只读
          disabled: true, // 置会
        },
        {
          type: 'INPUT_NUMBER',
          label: '旧件回运费用',
          fieldName: 'freightAmount',
          readonly: true, // 只读
          disabled: true, // 置会
        },
        {
          type: 'INPUT_NUMBER',
          label: '审核费用',
          fieldName: 'auditAmount',
          precision: 2,
          max: 99999999.99,
          min: 0,
          rules: [{ required: true, message: '请输入审核费用', trigger: 'change' }]
          // readonly: true, // 只读
          // disabled: true, // 置会
        }
      ]
    }
  }
};
</script>
<style lang="less" scoped></style>
